/*
 * SettingsPage.fx
 *
 * Created on Feb 25, 2010, 12:11:06 AM
 */
package com.gpsbuddy.navigation;

import javafx.scene.Scene;
import com.gpsbuddy.main.Enviroment;
import javafx.scene.control.CheckBox;
import javafx.scene.Group;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.HPos;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import com.gpsbuddy.utils.DatabaseUtils;
import com.gpsbuddy.data.Property;
import com.gpsbuddy.main.Main;

/**
 * @author jonycus
 */
public class SettingsPage extends Scene {

    public-init var env: Enviroment;
    var autoConnect = CheckBox {
                text: "Auto-connect on startup?"
            }
    var mainNavigationBar = MainStatusBar {
                env: env;
                width: bind env.screenWidth;
                height: bind env.screenHeight;
                barHeight: 30
            }
    var connectButton = Button {
                text: "OK";
                action: storeSettings;
            }
    var contentBox = VBox {
                layoutX: 20;
                layoutY: 30;
                nodeHPos: HPos.CENTER;
                spacing: 10;
                content: [autoConnect, connectButton]
            }
    var group = Group {
                layoutX: bind lX;
                opacity: bind op;
                content: [contentBox, mainNavigationBar]
            }

    postinit {
        if (DatabaseUtils.initialized) {
            fillStoredFields();
        }
        content = [group];
        }

    var op: Number = 0;
    var lX: Number = -env.screenWidth;

    public function transitIn() {
        Timeline {
            keyFrames: [
                KeyFrame {
                    time: 0s
                    values: [lX => -env.screenWidth tween Interpolator.LINEAR, op => 0.0 tween Interpolator.LINEAR];
                }
                KeyFrame {
                    time: 0.5s
                    values: [lX => 0 tween Interpolator.LINEAR, op => 1.0 tween Interpolator.LINEAR];
                }
            ]
        }.play();
    }

    public function storeSettings() {
        if (DatabaseUtils.hasProp("autoConnect")) {
            DatabaseUtils.updateProp("autoConnect", autoConnect.selected.toString());
        } else {
            DatabaseUtils.addProp("autoConnect", autoConnect.selected.toString());
        }
        Main.switchToFrontPage();
        DatabaseUtils.storeData();
    }

    function fillStoredFields() {
        for (prop: Property in DatabaseUtils.props) {
            if (prop.name.equals("autoConnect")) {
                autoConnect.selected = prop.value.equals("true");
            }
        }
    }

}
